//
//  RAPMenuBar.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 1/6/16.
//  Copyright © 2016 RendezvousAuParadis. All rights reserved.
//

import UIKit
import FDStackView

enum RAPMenuBarButtonType: Int {
    case Add = 1_000
    case Camera = 1_001
    case Emoticon = 1_002
    case Keyboard = 1_003
    case Mention = 1_004
}

@available(iOS 9.0, *)
class RAPMenuBar: FDStackView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 设置菜单栏的高度
        self.frame.size = CGSizeMake(kScreenBounds.size.width, 40)
        prepareToUse()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareToUse() {
        addArrangedSubview(buttonWithImageName("compose_add_background", buttonType: .Add))
        addArrangedSubview(buttonWithImageName("compose_toolbar_picture", buttonType: .Camera))
        addArrangedSubview(buttonWithImageName("compose_emoticonbutton_background", buttonType: .Emoticon))
        addArrangedSubview(buttonWithImageName("compose_trendbutton_background", buttonType: .Keyboard))
        addArrangedSubview(buttonWithImageName("compose_mentionbutton_background", buttonType: .Mention))
        
        // 按钮均匀排布
        distribution = .FillEqually
    }
    
    private func buttonWithImageName(imageName: String, buttonType: RAPMenuBarButtonType) -> UIButton {
        let button = UIButton(title: nil, fontSize: 14, imageName: imageName, backgroundImageName: "compose_toolbar_background", target: self, action: "menuButtonClicked:")
        button.adjustsImageWhenHighlighted = false
        button.tag = buttonType.rawValue
        return button
    }
    
    // MARK: - 动作方法
    @objc private func menuButtonClicked(sender: UIButton) {
        buttonClickHandler?(buttonType: RAPMenuBarButtonType(rawValue: sender.tag)!)
    }
    
    // MARK: - 自定义属性
    var buttonClickHandler: ((buttonType: RAPMenuBarButtonType)->())?
}
